/*!
 * @file        apm32f00x_int.c
 *
 * @brief       Main Interrupt Service Routines
 *
 * @version     V1.0.0
 *
 * @date        2019-11-4
 *
 */

#include "apm32f00x.h"
#include "apm32f00x_adc.h"
#include "apm32f00x_int.h"
#include "apm32f00x_eint.h"
#include "main.h"
#include "UsrUsart.h"


typedef void (*INTERRUPT_FUNCTION)(void);

#if defined(__CC_ARM)

/* Indicates that the current interrupt vector */
uint32_t FLASH_interrupt_vector __attribute__((at(0x20000004))) = IAP_INTERRUPT_VECTOR;

/* application interrupt vector address */
INTERRUPT_FUNCTION APP_interrupt_vector_addr __attribute__((at(0x20000000))) = (INTERRUPT_FUNCTION)0;

#elif defined(__ICCARM__)
#pragma location = 0x20000000
__root INTERRUPT_FUNCTION APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)0;
;
#pragma location = 0x20000004
__root uint32_t FLASH_interrupt_vector = IAP_INTERRUPT_VECTOR;

#else
/* application interrupt vector address */
INTERRUPT_FUNCTION APP_interrupt_vector_addr __attribute__((section(".my_data"))) = (INTERRUPT_FUNCTION)0;

/* Indicates that the current interrupt vector */
uint32_t FLASH_interrupt_vector __attribute__((section(".my_data"))) = IAP_INTERRUPT_VECTOR;

#endif

/**@} end of group BootLoader_INT_Variables */

/** @defgroup BootLoader_INT_Functions INT_Functions
  @{
*/

/*!
 * @brief       This function handles NMI exception
 *
 * @param       None
 *
 * @retval      None
 */
void NMI_Handler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_NonMaskableInt_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles Hard Fault exception
 *
 * @param       None
 *
 * @retval      None
 */
void HardFault_Handler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_HardFault_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles SVCall exception
 *
 * @param       None
 *
 * @retval      None
 */
void SVC_Handler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_SVC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles PendSV_Handler exception
 *
 * @param       None
 *
 * @retval      None
 */
void PendSV_Handler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_PendSV_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles SysTick exception
 *
 * @param       None
 *
 * @retval      None
 */
void SysTick_Handler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    sysTick++;
    if (__delayCnt)
      __delayCnt--;
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_SysTick_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles WUPT exception
 *
 * @param       None
 *
 * @retval      None
 */
void WUPT_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_WUPT_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles RCM exception
 *
 * @param       None
 *
 * @retval      None
 */
void RCM_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_RCM_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles EINTA exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTA_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    EINT_ClearIntFlag(EINT_PORT_A);
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_EINTA_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles EINTB exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTB_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    EINT_ClearIntFlag(EINT_PORT_B);
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_EINTB_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles EINTC exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTC_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    //GPIO_IRQHandler();
    EINT_ClearIntFlag(EINT_PORT_C);
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_EINTC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles EINTD exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTD_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    EINT_ClearIntFlag(EINT_PORT_D);
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_EINTD_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles SPI exception
 *
 * @param       None
 *
 * @retval      None
 */
void SPI_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_SPI_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles TIMER1 update/overflow/underflow/trigger/break exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_UT_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR1_UT_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles TIMER1 capture/compare exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_CC_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR1_CC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles TIMER2 update /overflow exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR2_UO_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR2_UO_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles TIMER2 update /overflow exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR2_CC_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR2_CC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles USART1 TX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART1_TX_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    //Usr_USART1_TX_IRQ();
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_USART1_TX_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles USART1 RX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART1_RX_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    //Usr_USART1_RX_IRQ();
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_USART1_RX_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles USART3 TX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART3_TX_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    //Usr_USART3_TX_IRQ();
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_USART3_TX_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles USART3 RX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART3_RX_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
    Usr_USART3_RX_IRQ();
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_USART3_RX_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles I2C exception
 *
 * @param       None
 *
 * @retval      None
 */
void IIC_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_IIC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles ADC exception
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_ADC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles TIMER4 update /overflow exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR4_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR4_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles Flash exception
 *
 * @param       None
 *
 * @retval      None
 */
void FLASH_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_FLASH_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles USART2 TX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART2_TX_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_USART2_TX_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles USART2 RX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART2_RX_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_USART2_RX_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}


/*!
 * @brief       This function handles TIMER1A update/overflow/underflow/trigger/break exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1A_UT_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR1A_UT_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/*!
 * @brief       This function handles TIMER1A capture/compare exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1A_CC_IRQHandler(void)
{
  if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
  {
  }
  else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
  {
    APP_interrupt_vector_addr = (INTERRUPT_FUNCTION) * (uint32_t *)APP1_TMR1A_CC_IRQn_APP_ADDR;
    APP_interrupt_vector_addr();
  }
}

/**@} end of group BootLoader_INT_Functions */
/**@} end of group BootLoader */
/**@} end of group Examples */
